package com.example.tool.activtiy;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.transition.TransitionManager;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.tool.R;
import com.example.tool.activtiy.base.BaseActivity;
import com.example.tool.bean.ScanInfo;
import com.example.tool.mvp.CommonPresent;
import com.example.tool.util.DataRequest;
import com.example.tool.util.DebugLog;
import com.example.tool.widget.VirusKilling;

import java.util.List;
import java.util.Random;

import butterknife.BindView;

import static com.example.tool.util.DebugLog.VIEW_360;

/**
 * Created Date 2019/12/1.
 *
 * @author liangkun
 * ClassDescription：
 */
public class Kill360Activity extends BaseActivity<CommonPresent> {

    private static final int SCANING = 0;
    private static final int SCANINGFINISH = 1;
    @BindView(R.id.tv_title)
    protected TextView tvTitle;
    @BindView(R.id.tv_scan)
    protected TextView tv_scan;
    @BindView(R.id.progressbar)
    protected ProgressBar progressBar;
    @BindView(R.id.ll_add_text)
    protected LinearLayout llAddText;
    @BindView(R.id.vk_scan_virus)
    VirusKilling vkScanVirus;
    private int index;
    private int sce;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SCANING:
                    progressBar.setProgress(index);
                    ScanInfo info = (ScanInfo) msg.obj;
                    tv_scan.setText(info.getFileName());
                    TextView textView = new TextView(getApplicationContext());
//                    textView.setGravity(Gravity.CENTER);
                    textView.setTextSize(getResources().getDimensionPixelSize(R.dimen.sw_sp_10));
                    if (info.getHasFace().equals("HASFACE")) {
                        textView.setTextColor(Color.RED);
                        textView.setText("发现人脸" + info.getFilePath());
                    } else {
                        textView.setTextColor(Color.BLACK);
                        textView.setText("无人脸" + info.getFileName());
                    }
                    textView.setLayoutParams(params);
                    //把view加到 LinearLayout中
                    llAddText.addView(textView, 0);
                    break;
                case SCANINGFINISH:
                    tv_scan.setText("图片处理完成");
                    vkScanVirus.stopScan();
                    progressBar.setProgress(0);

                    break;
            }
        }
    };
    private List<ScanInfo> picInfoBeans;
    private LinearLayout.LayoutParams params;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_kill;
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    protected void initView() {
        tvTitle.setText("图片处理完成");
        initParams();
        //不显示
        vkScanVirus.setSchedule(sce++, false);
        picInfoBeans = DataRequest.getPicInfoBean(500);
//        progressBar.setMax(picInfoBeans.size());
        progressBar.setMax(100);
        DebugLog.d(VIEW_360 + " picInfoBeans " + picInfoBeans.size());
    }

    private void initParams() {
        params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.height = getResources().getDimensionPixelSize(R.dimen.sw_dp_30);
        params.width = LinearLayout.LayoutParams.MATCH_PARENT;
        params.topMargin = getResources().getDimensionPixelOffset(R.dimen.sw_dp_5);
    }

    @Override
    protected void initData() {
        vkScanVirus.startScanning();
        //进度条
//        sce = 0;
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    if (sce > 100) {
//                        sce = 0;
//                    }
//                    try {
//                        Thread.sleep(200);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
////                    vkScanVirus.setSchedule(sce++);
////
//                }
//            }
//        }).start();
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                //模拟人脸检测
                vkScanVirus.startScanning();
                scanFace();
            }
        }, 3000);

    }


    /**
     * 扫描
     */
    private void scanFace() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < picInfoBeans.size(); i++) {
                    ScanInfo scanInfo = picInfoBeans.get(i);
                    //在子线程中发送消息更新UI
//                    index++;
                    index = new Random().nextInt(100) + 1;
                    try {
                        Thread.sleep(500 + new Random().nextInt(100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Message msg = Message.obtain();
                    msg.what = SCANING;
                    msg.obj = scanInfo;
                    if (mHandler == null) {
                        break;
                    }
                    mHandler.sendMessage(msg);
                }
                if (mHandler != null) {
                    //结束标志
                    Message msg = Message.obtain();
                    msg.what = SCANINGFINISH;
                    mHandler.sendMessage(msg);
                }

            }
        }).start();

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (mHandler != null) {
            mHandler.removeCallbacks(null);
            mHandler = null;
        }
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected CommonPresent createPresenter() {
        return null;
    }
}
